/**
 * 
 */
package com.chumi.bootstrap.controller.demo;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chumi.bootstrap.mapper.standard.tsys.TSysUserMapper;
import com.chumi.bootstrap.model.standard.tsys.TSysUser;
import com.chumi.bootstrap.service.demo.DemoService;
import com.chumi.bootstrap.service.demo.DemoServiceNoExtends;
import com.chumi.bootstrap.service.tsys.TSysUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.llsfw.core.controller.base.BaseController;
import com.llsfw.core.datasource.DBContextHolder;
import com.llsfw.core.exception.SystemException;
import com.llsfw.generator.model.standard.system.TtDynamicDataSource;

/**
 * @author Administrator
 *
 */
@Controller
public class DemoLoginController extends BaseController {

    /**
     * <p>
     * Field log: 日志
     * </p>
     */
    private static final Logger LOG = LogManager.getLogger();

    @Autowired
    private DemoService ds;

    @Autowired
    private DemoServiceNoExtends dsne;
    
   

    @RequestMapping("jsontest")
    @ResponseBody
    public TtDynamicDataSource jsontest() throws SystemException {
        TtDynamicDataSource ds = new TtDynamicDataSource();
        ds.setDbsName("我是中文");
        return ds;
    }

    @RequestMapping("pageQuery")
    public String pageQuery() throws SystemException {
        TtDynamicDataSource dataSource = null;
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_1");
        DBContextHolder.setDBType(dataSource);
        ds.pageQuery();

        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.pageQuery();

        return "main";
    }

    @RequestMapping("testLang")
    public String testLang() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        TestValidator entity = new TestValidator();
        entity.setAge(12);
        entity.setName("admin");
        Set<ConstraintViolation<TestValidator>> constraintViolations = validator.validate(entity);
        for (ConstraintViolation<TestValidator> constraintViolation : constraintViolations) {
            System.out.println("对象属性:" + constraintViolation.getPropertyPath());
            System.out.println("国际化key:" + constraintViolation.getMessageTemplate());
            System.out.println("错误信息:" + constraintViolation.getMessage());
        }
        return "testLang";
    }

    @RequiresPermissions("test:add")
    @RequestMapping("/t/a")
    public String aa() {
        return "main";
    }

    @RequestMapping("/testTransactionalNoExtendsBaseService")
    public String testTransactionalNoExtendsBaseService() throws Exception {
        dsne.insertTtScheduledLog();
        dsne.testCallSql();
        return "main";
    }

    @RequestMapping("/testTransactional")
    public String testTransactional() throws Exception {
        ds.insertTtScheduledLog();
        ds.testCallSql();
        return "main";
    }

    @RequestMapping("/testTtDynamicDataSource")
    public String testTtDynamicDataSource() throws Exception {
        LOG.fatal("fatal");
        LOG.error("error");
        LOG.warn("warn");
        LOG.info("info");
        LOG.debug("debug");
        LOG.trace("trace");
        ds.insertTtScheduledLog();
        ds.testCallSql();

        TtDynamicDataSource dataSource = null;

        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_1");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        ds.testCallSql();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_1");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        ds.testCallSql();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        ds.testCallSql();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        DBContextHolder.setDBType(null);
        ds.testCallSql();

        DBContextHolder.setDBType(null);
        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_2");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        dataSource = this.getDynamicDataSoucre("LLSFW_DEMO_1");
        DBContextHolder.setDBType(dataSource);
        ds.testReadDemo();

        return "main";
    }

    @RequestMapping("/")
    public String toMainPage() {
        return "main";
    }

    @RequestMapping(value = "/login")
    public String login(HttpServletRequest req) {
        String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
        String rv = null;
        if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
            rv = "用户名或者密码错误";
        } else if (LockedAccountException.class.getName().equals(exceptionClassName)) {
            rv = "用户被锁定";
        } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            rv = "用户名或者密码错误";
        } else if (exceptionClassName != null) {
            rv = "未知错误,请联系管理员";
            LOG.info(exceptionClassName);
        }
        req.setAttribute("rv", rv);
        return "login";
    }
    
    

}
